<?php
//seniority file
if(!checkQualified("1000")) {
	print "You are not allowed to perform this action! (You=" . $_SESSION[permission] . " : Req'd=1000)";
} else {
	
	require_once(INCLUDEFOLDER . "seniority.class.php");

	switch($_REQUEST['seniority']) {
/*******************************************************/
/*             Point Adjustment History                */ 
/*******************************************************/
		case 'history':
			$pointsHistory = new PointsHistory();
			$pointsHistory->getTotalRows();
			$pointsHistory->setOffset(30);
			$pointsHistory->setCurrentPage($_GET['page']);
?>
<div style="text-align:center"><h1>Seniority Point Adjustment History</h1></div>
<p style="text-align: right; padding-right: 10px; font-weight: bold">
<? $pointsHistory->showButtons(); ?>
</p>
<table cellpadding="2" width="100%">
	<tr>
		<th align="center">Driver</th>
		<th align="center">Added By</th>
		<th align="center">Adjustment</th>
		<th align="center">Description</th>
		<th align="center">Date</th>
	</tr>
<?php $pointsHistory->showHistory(); ?>
</table>
<?
		break;

/*******************************************************/
/*                Pay Period Data Form                 */
/*******************************************************/
		case 'payperiod':

$points = new PointsPayPeriod();
if (isset($_POST['submit'])) {
	$points->setEnteredBy($_SESSION['driver_id']);
	$points->getData();
	$validate = $points->validateData();
	if ($validate) $points->insertData();
}
?>
<div style="text-align:center"><h1>Pay Period Data</h1></div>
<script type="text/javascript">
function totalDriverPoints(id){
	single = document.forms[0].elements['points[' + id + ']'].value;
	double = document.forms[0].elements['doublePoints[' + id + ']'].value;
	triple = document.forms[0].elements['triplePoints[' + id + ']'].value;
	quadruple = document.forms[0].elements['quadruplePoints[' + id + ']'].value;
	
	trId = "row" + id;
	
	if (isNaN(single) || isNaN(double) || isNaN(triple) || isNaN(quadruple)){
		document.getElementById(trId).style.backgroundColor = "FFCCCC";
	} else {
		document.getElementById(trId).style.backgroundColor = "FFFFFF";
	}
	
	if (!(single == "" && double == "" && triple == "" && quadruple == "")){
		if (!isNaN(single) && single != "") single = single * 1;
		else single = 0;
	
		if (!isNaN(double) && double != "") double = 2 * double;
		else double = 0;
		
		if (!isNaN(triple) && triple != "") triple = 3 * triple;
		else triple = 0;
		
		if (!isNaN(quadruple) && quadruple != "") quadruple = 4 * quadruple;
		else quadruple = 0;

		totalPoints = (single + double + triple + quadruple);
		spanID = "totalPoints" + id;

		document.getElementById(spanID).innerHTML = totalPoints;
	}
}
</script>

<center>
<?
if ($validate){
?>
<div>
The pay period data has successfully been entered into the database. You may now do one of the following:<br /><br />
<a href="?seniority=generate">Generate a seniority list</a><br />
<a href="?seniority=adjust">Add points for write ups/happy write ups</a><br />
<a href="?seniority=history">View adjustment history</a><br />
</div>
<?
} else {
?>
<form action="?seniority=payperiod" method="post">

<div style="padding-bottom: 20px">Pay Period Ending <? $points->displayPayPeriod(); ?></div>

<?
if ($points->getErrors()){
?>
<div class="error">There is an error in some of the data entered.<br />Please see the red fields below.</div> 
<?
}
?>

<div>
<table border="0" cellspacing="0" cellpadding="2">
<tr>
	<th>Driver</th>
	<th>Points</th>
	<th>Double<br />Points</th>
	<th>Triple<br />Points</th>
	<th>Quad<br />Points</th>
	<th>Total<br />Points</th>
</tr>
<? $points->displayActiveDriverForm(); ?>
</table>
</div>
<input type="submit" name="submit" value="Submit" />
</form>
<?
}
?>
</center>
<?
		break;
/*******************************************************/
/*             Point Adjustments Page                  */
/*******************************************************/		
		case 'adjust':
$points = new PointsAdd();
if (isset($_POST['submit'])) {
	$points->setEnteredBy($_SESSION['driver_id']);
	$points->getData();
	$validate = $points->validateData();
	if ($validate) $insertMessages = $points->insertData();
}

$points->displayActiveDriverJavascript();
?>
<div style="text-align:center"><h1>Seniority Point Adjustments</h1></div>
<script type="text/javascript">

function totalDriverPoints(id){
	single = document.forms[0].elements['points[' + id + ']'].value;
	double = document.forms[0].elements['doublePoints[' + id + ']'].value;
	triple = document.forms[0].elements['triplePoints[' + id + ']'].value;
	quadruple = document.forms[0].elements['quadruplePoints[' + id + ']'].value;
	
	trId = "row" + id;
	
	if (isNaN(single) || isNaN(double) || isNaN(triple) || isNaN(quadruple)){
		document.getElementById(trId).style.backgroundColor = "FFCCCC";
	} else {
		document.getElementById(trId).style.backgroundColor = "FFFFFF";
	}
	
	if (!(single == "" && double == "" && triple == "" && quadruple == "")){
		if (!isNaN(single) && single != "") single = single * 1;
		else single = 0;
	
		if (!isNaN(double) && double != "") double = 2 * double;
		else double = 0;
		
		if (!isNaN(triple) && triple != "") triple = 3 * triple;
		else triple = 0;
		
		if (!isNaN(quadruple) && quadruple != "") quadruple = 4 * quadruple;
		else quadruple = 0;

		totalPoints = (single + double + triple + quadruple);
		spanID = "totalPoints" + id;

		document.getElementById(spanID).innerHTML = totalPoints;
	}
}

function showEmailField(){
	if (document.forms[0].sendEmail.checked == true || document.forms[0].sendMessage.checked == true)
		document.getElementById("emailDriver").style.display = "block";
	else
		document.getElementById("emailDriver").style.display = "none";
}

function getDriverName(){
	driverid = document.forms[0].driver.value;
	return drivers[driverid];
}

function getDriverPoints(){
	return document.forms[0].points.value;
}
</script>
<center>
<form action="?seniority=adjust" method="post">
<?
if ($points->getErrors()) {
	echo "<div class=\"error\">";
	echo $points->showErrors();
	echo "</div>";
}
if ($insertMessages) {
	echo "<div class=\"error\">";
	echo $insertMessages;
	echo "</div>";
}
?>

<div>
<table border="0" cellspacing="0" cellpadding="2" border="1">
<tr>
	<td align="right">Driver: </td>
	<td><? $points->displayActiveDriverList($points->getDriverID()); ?></td>
</tr>
<tr>
	<td valign="top" align="right">Reason: </td>
	<td><textarea rows="3" cols="30" name="reason"><?php echo $points->getReason(); ?></textarea></td>
</tr>
<tr>
	<td align="right">Points: </td>
	<td><input type="text" name="points" size="15" value="<?php echo $points->getPoints(); ?>" onClick="this.select()" /> <img src="images/questionmark.gif" align="absmiddle" onMouseOver="this.T_WIDTH=250; this.T_TITLE='Points'; return escape('It is <u>not</u> necessary to enter a plus sign (+) when increasing a driver\'s points (ex: +10.75), however, when decreasing a driver\'s points, you <b>must</b> enter a negative sign (ex: -15.25).')" /></td>
</tr>
<tr>
	<td align="right"><input type="checkbox" name="resetFields" value="1"<?php echo $points->getResetFields() ?> /></td>
	<td>Reset fields after submit? <img src="images/questionmark.gif" align="absbottom" onMouseOver="this.T_WIDTH=250; this.T_TITLE='Reset Fields'; return escape('If this field is checked, all of the fields will be cleared after the form is submitted. With the box unchecked, all of the data will remain in the fields for the next adjustment. This is useful, for example, when adding multiple happy write ups and the only difference is the drivers name.')" /></td>
</tr>
<tr>
	<td align="right"><input type="checkbox" name="sendMessage" value="1" onClick="showEmailField()"<?php echo $points->getSendMessage(); ?> /></td>
	<td>Send message to driver? <img src="images/questionmark.gif" align="absbottom" onMouseOver="this.T_WIDTH=250; this.T_TITLE='Send Message to Driver'; return escape('This will send a message to the driver, which will be visible via Message Center on the main page.')" /></td>
</tr>
<tr>
	<td  align="right"><input type="checkbox" name="sendEmail" onClick="showEmailField()" value="1"<?php echo $points->getSendEmail(); ?> /></td>
	<td>Send email to driver?</td>
</tr>
</table>
<div id="emailDriver">
Subject: <input type="text" name="emailSubject" size="30" value="<?php echo $points->getSubject();?>" /><br>
<textarea name="emailBody" id="emailBody" rows="8" cols="80"><?php echo $points->getBody(); ?></textarea>
</div>
</div>
<input type="submit" name="submit" value="Submit" />
</form>
<script type="text/javascript">
getDriverName();
showEmailField();
</script>
<script language="JavaScript" type="text/javascript" src="../tooltip.js"></script>
</center>
<?
		break;
/*******************************************************/
/*            Generate Seniority List                  */
/*******************************************************/
		case 'generate':
$seniorityList = new SeniorityList();
$seniorityList->getNewestGroup();
?>
<div style="text-align:center; "><h1>Generate Seniority List</h1></div>
<center>
<?
if (isset($_POST['generateList'])){
	echo $seniorityList->generateList();
} else {
?>
<p>You should only generate a seniority list when you want to print it out for display. <br><strong>DO NOT</strong> generate a seniority list just to view the current list.</p>

<?
$newestList = $seniorityList->getNewestSeniorityListDate();
if ($newestList) echo "<p>The last seniority list was generated on <strong>" . $newestList . "</strong>. Are you sure you want to generate another one?";
?>
<form action="?seniority=generate" method="post">
	<input type="submit" name="generateList" value="Generate Seniority List">
</form>
<?
}
?>
</center>
<?
		break;
/*******************************************************/
/*           View List of Seniority Lists              */
/*******************************************************/
		case 'view':
?>
<div style="text-align:center"><h1>Seniority Lists</h1></div>
<center>
<p>By default, when you print out these seniority lists, or any other web page for that matter, a header and<br />
footer will also be printed, including the page name, url, number, and date. To prevent these from being<br />
displayed, you should see some Header/Footer options by going to File &gt; Page Setup in your browser.</p>
<?
$displayLists = new SeniorityList();
$displayLists->displayLists();
?>
<center>
<?
		break;
/*******************************************************/
/*            Default Case: Show Menu                  */
/*******************************************************/
		default:
?>
<div style="text-align:center"><h1>Seniority Points Options</h1></div>
<ul>
	<li><a href="<?= $_SERVER['PHP_SELF'] ?>?seniority=payperiod">Enter Pay Period Data</a></li>
	<li><a href="<?= $_SERVER['PHP_SELF'] ?>?seniority=adjust">Enter Point Adjustments</a></li>
	<li><a href="<?= $_SERVER['PHP_SELF'] ?>?seniority=history">View Adjustment History</a></li>
	<li><a href="<?= $_SERVER['PHP_SELF'] ?>?seniority=generate">Generate Seniority List</a></li>
	<li><a href="<?= $_SERVER['PHP_SELF'] ?>?seniority=view">View Seniority Lists</a></li>
</ul>
	
<?
	} // end switch
} //end checkQualified if
?>